package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.Button;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.App;
import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.WalletEntity;
import com.wuliupai.delivery.wuliupaidelivery.constants.InterfaceNet;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.utils.DateUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.SharePreferenceUtil;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.Date;

@EActivity(R.layout.activity_wallet_balance)
public class WalletBalanceActivity extends AppBaseActivity {

    @Bean(SharePreferenceUtil.class)
    SharePreferenceUtil mSharePreferenceUtil;
    @Bean(DateUtils.class)
    DateUtils mDateUtils;

    @ViewById(R.id.tv_wallet_balance_my_balance)
    TextView tv_wallet_balance_my_balance; //余额界面

    @ViewById(R.id.btn_wallet_balanet_deposit)
    Button btn_wallet_balanet_deposit;


    @Click(R.id.btn_wallet_balanet_recharge)
    void balanetRecharge() { //点击充值
        //判断用户是否绑定银行卡 绑定跳转充值界面 没有绑定跳转银行卡绑定界面
        int whetherBank = mSharePreferenceUtil.getWhetherBank();
        if (whetherBank == 0) {
            WalletAddBankCard_.intent(this).pagerTitle(0).start();
        } else if (whetherBank == 1) {
            WalletBalancetChargeActivity_.intent(this).PagerType(1).start();
        }
    }

    @Click(R.id.btn_wallet_balanet_deposit)
    void balanetDeposit() { //点击提现
        //判断用户是否绑定银行卡 绑定跳转充值界面 没有绑定跳转银行卡绑定界面
        //是否绑定了银行卡 0：未绑定  1：已绑定
        int whetherBank = mSharePreferenceUtil.getWhetherBank();
        if (whetherBank == 0) {//没有绑定银行卡
            WalletAddBankCard_.intent(this).pagerTitle(0).start();
        } else if (whetherBank == 1) {//已绑定银行卡 提现界面
            if (mDateUtils.IfCurrentTimeConform(new Date())) {
                WalletBalancetChargeActivity_.intent(this).PagerType(2).start();
            } else {
                App.toast("请于每周一 9点至18点间进行提现操作");
            }
        }
    }

    @AfterViews
    void onPageStart() {
        LogUtils.log("");
        setDefaultTitle(this, "余额");
        if (!mDateUtils.IfCurrentTimeConform(new Date())) {
            btn_wallet_balanet_deposit.setBackgroundResource(R.drawable.rect_black_not_click);
            btn_wallet_balanet_deposit.setTextSize(15);
            //btn_wallet_balanet_deposit.setTextColor(getResources().getColor(R.color.color_defaultTextLight));
            String s = "提现 (每周一 9:00-18:00 开放提现)";
            SpannableStringBuilder builder = new SpannableStringBuilder(s);
            int defaultTe = getResources().getColor(R.color.color_textBlueDeep);
            ForegroundColorSpan greenSpan = new ForegroundColorSpan(defaultTe);
            builder.setSpan(greenSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            btn_wallet_balanet_deposit.setText(builder);
            btn_wallet_balanet_deposit.setEnabled(false);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        getTotal();//每次显示界面 便获取余额数据
    }

    // 获取账户余额
    private void getTotal() {
//        String aesBillList = null;
        WalletEntity entity = new WalletEntity(this);
        String walletJson = JsonUtils.createJsonString(entity);
        try {
            String aes = JsonUtils.aes(walletJson);
            callNet(this, InterfaceNet.Net_GetWalletTotalMoney, aes, new NetResultOperate() {
                @Override
                public void onSuccess(String content) {
                    if (!StringUtils.isEmpty(content)) {
                        WalletEntity walletEntity = (WalletEntity) JsonUtils.parseJsonString(content, WalletEntity.class);
                        String totalMoney = StringUtils.format2Yuan(walletEntity.getTotalMoney());
                        tv_wallet_balance_my_balance.setText("¥" + totalMoney);
                    }
                }

                @Override
                public void onDataEmpty(String content) {

                }

                @Override
                public void onDataEnd(String content) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        animUtils.leftOutAnimation(this);
    }
}